<?php
	
	
	require_once("view/SignUpView.php");
	
	
	class SignUpController{
			
		private $signUpView;
		private $userHandler;
		private $user;
		
		public function __construct(UserHandler $userHandler){
			$this->signUpView = new SignUpView();
			$this->userHandler = $userHandler;
		}
		
		public function doControll($a_isAdmin = false){
			$page = new Page();
			//Handle input
			if($this->signUpView->didUserClickSubmit() === true){
				//if Form is ok, try to create a new member
				if($this->signUpView->isFormValid($a_isAdmin) === true){
					$this->user = $this->userHandler->insertUser($this->signUpView->getUserCredentials());
				}
			}
			
			//Handle output
			if(is_object($this->user)){
				//The user was inserted into tabel, redirect to login page
				NavigationView::setHeadeLocation("index.php?" . NavigationView::controller . "=" . NavigationView::login . "&" . NavigationView::username . "=" . $this->user->UserName);
			}else{
				if($this->user === false){
					//The username was not available, set error mesage
					$this->signUpView->SetErrorUserNameExisted();
				}
				$page->body = $this->signUpView->generateSignUpBox($a_isAdmin);
			}
			$page->title = "YAIC | Sign up";
			return $page;
		}
	}
